package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.CommConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.support.JacksonUtil;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.merchant.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.merchant.service.MerchantBillService;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户端账单
 *
 * @author qiguliuxing
 */
@RestController
@RequestMapping("/merchant/bill")
@Validated
@Api(tags = ApiAdminConsts.MerchantBill.PARENT)
public class MerchantBillController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantBillController.class);

    @Autowired
    private MerchantBillService merchantBillService;

    /**
     * 查询日统计账单
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("merchant:bill:list")
    @RequiresPermissionsDesc(menu = {"财务管理", "账单管理"}, button = "查询")
    @ApiOperation(value = ApiAdminConsts.MerchantBill.LIST)
    @WebLog(description = ApiAdminConsts.MerchantBill.LIST, excludes = {LogParamsConsts.RESPONSE})
    public Object list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
                       @ApiParam(value = "时间范围") @RequestParam("dateRange") List<String> dateRange,
                       @ApiParam(value = "排序字段", defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @ApiParam(value = "排序规则", defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {

        Integer brandId = AuthSupport.merchantBrandId();
        return merchantBillService.listBill(page, limit, sort, order, brandId, dateRange);
    }

    /**
     * 商户统计账户总览
     *
     * @return
     */
    @GetMapping("/overview")
    @RequiresPermissions("merchant:bill:overview")
    @RequiresPermissionsDesc(menu = {"财务管理", "账单管理"}, button = "账号总览")
    @ApiOperation(value = ApiAdminConsts.MerchantBill.OVERVIEW)
    @WebLog(description = ApiAdminConsts.MerchantBill.OVERVIEW)
    public Object overview() {
        Integer brandId = AuthSupport.merchantBrandId();
        return merchantBillService.overview(brandId);
    }

    /**
     * 商户资金提现
     *
     * @return
     */
    @PostMapping("/apply")
    @RequiresPermissions("merchant:bill:overview")
    @RequiresPermissionsDesc(menu = {"财务管理", "账单管理"}, button = "提现申请")
    @ApiOperation(value = ApiAdminConsts.MerchantBill.APPLY)
    @WebLog(description = ApiAdminConsts.MerchantBill.APPLY, persistence = true)
    public Object apply(@ApiParam(value = "{withdrawType:'支付方式',amt:'提现金额'}") @RequestBody String body) {
        Integer brandId = AuthSupport.merchantBrandId();
        if (brandId == null) {
            logger.error("提现申请出错，当前登录用户商户为空！");
            return ResponseUtils.serious();
        }

        Integer withdrawType = JacksonUtil.parseInteger(body, "withdrawType");
        String amt = JacksonUtil.parseString(body, "amt");

        if (StringUtils.isEmpty(amt)) {
            logger.error("提现申请失败:{}", CommConsts.MISS_PARAMS);
            return ResponseUtils.badArgument();
        }

        return merchantBillService.apply(brandId, withdrawType, amt);
    }
}
